"""
Scrapy设置文件
"""

# Scrapy settings for myscraper project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = "myscraper"

SPIDER_MODULES = ["myscraper.spiders"]
NEWSPIDER_MODULE = "myscraper.spiders"

ADDONS = {}

# 爬虫基本设置
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
ROBOTSTXT_OBEY = True  # 遵守robots.txt规则
CONCURRENT_REQUESTS = 16  # 并发请求数
DOWNLOAD_DELAY = 0.5  # 下载延迟，单位为秒
RANDOMIZE_DOWNLOAD_DELAY = True  # 随机化下载延迟
COOKIES_ENABLED = False  # 禁用cookies
TELNETCONSOLE_ENABLED = False  # 禁用telnet控制台

# 重试设置
RETRY_ENABLED = True
RETRY_TIMES = 3  # 最大重试次数
RETRY_HTTP_CODES = [500, 502, 503, 504, 522, 524, 408, 429]  # 需要重试的HTTP状态码

# 超时设置
DOWNLOAD_TIMEOUT = 10  # 下载超时时间，单位为秒

# 启用的中间件
DOWNLOADER_MIDDLEWARES = {
    "scrapy.downloadermiddlewares.useragent.UserAgentMiddleware": None,
    "scrapy.downloadermiddlewares.retry.RetryMiddleware": 500,
    "scrapy.downloadermiddlewares.redirect.RedirectMiddleware": 600,
}

# 启用的Pipeline
ITEM_PIPELINES = {
    "myscraper.pipelines.DefaultValuesPipeline": 300,
    "myscraper.pipelines.DuplicatesPipeline": 400,
    "myscraper.pipelines.JsonWriterPipeline": 800,
}

# 日志设置
LOG_LEVEL = "INFO"  # 日志级别
LOG_FILE = "logs/scrapy.log"  # 日志文件路径
LOG_ENABLED = True  # 启用日志
LOG_ENCODING = "utf-8"  # 日志编码
LOG_DATEFORMAT = "%Y-%m-%d %H:%M:%S"  # 日志日期格式
LOG_FORMAT = "%(asctime)s [%(name)s] %(levelname)s: %(message)s"  # 日志格式

# 输出设置
FEED_EXPORT_ENCODING = "utf-8"  # 输出编码
FEED_EXPORT_INDENT = 2  # JSON输出缩进

# 自定义设置
JSON_OUTPUT_DIR = "data/output"  # JSON输出目录
MONGO_URI = "mongodb://localhost:27017"  # MongoDB连接URI
MONGO_DATABASE = "myscraper"  # MongoDB数据库名

# 请求头设置
DEFAULT_REQUEST_HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    "myscraper.middlewares.MyscraperSpiderMiddleware": 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    "myscraper.middlewares.MyscraperDownloaderMiddleware": 543,
#}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    "scrapy.extensions.telnet.TelnetConsole": None,
#}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = "httpcache"
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = "scrapy.extensions.httpcache.FilesystemCacheStorage"

# Set settings whose default value is deprecated to a future-proof value
FEED_EXPORT_ENCODING = "utf-8"
